﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Security;
using LuckyLoot.Common.Membership;
using LuckyLoot.ServiceFacade.Contracts;
using LuckyLoot.ServiceFacade.Implementation;

namespace LuckyLoot.Presentation
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {

        private IUserService _userService;
        public IUserService UserService
        {
            get
            {
                if (_userService == null)
                    _userService = new UserService();

                return _userService;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {
            var user = Page.User;

            if (!user.Identity.IsAuthenticated)
            {
                this.NavigationMenu.Visible = false;
            }
            else
            {
                var luckylootUser = this.UserService.GetUser(user.Identity.Name);
                if (luckylootUser == null)
                {
                    FormsAuthentication.SignOut();
                    FormsAuthentication.RedirectToLoginPage(string.Empty);
                }

                if (luckylootUser.IsAdmin)
                {
                    //<asp:MenuItem NavigateUrl="~/User.aspx" Text="Usuário"/>
                    this.NavigationMenu.Items.Add(new MenuItem() { Text = "Usuário", NavigateUrl = "~/User.aspx" });
                }
            }
        }

        public void ShowErrorMessage(string message)
        {
            HideMessages();

            this.ErrorMessagePanel.Controls.Add(new HtmlGenericControl("span") { InnerText = message });
            this.ErrorMessagePanel.Visible = true;
        }

        public void ShowInfoMessage(string message)
        {
            HideMessages();

            

            this.InfoMessagePanel.Controls.Add(new HtmlGenericControl("span") { InnerText = message });
            this.InfoMessagePanel.Visible = true;
        }

        public void ShowSuccessMessage(string message)
        {
            HideMessages();

            this.SuccessMessagePanel.Controls.Add(new HtmlGenericControl("span") { InnerText = message });
            this.SuccessMessagePanel.Visible = true;
        }

        public void HideMessages()
        {

            this.InfoMessagePanel.Visible = false;
            this.ErrorMessagePanel.Visible = false;
            this.SuccessMessagePanel.Visible = false;
        }
    }
}
